﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ResourceRequest.Business;
using ePlanet.ResourceRequest.Services;
using Microsoft.Practices.Unity;
using Web.SystemLogin.Services;
using Web.SystemConfig.Services;
using Web.SystemLogin.Models;
using Web.SystemLogin.Business;
using DreamBuilder.Core.Log;
using DreamBuilder.Core.Container;
using System.Threading;
using System.Web.Security;
using DreamBuilder.Common;

namespace Web.Views.TempUserManage
{
    public partial class TempUserLogin : PageBase
    {
        [Dependency]
        public ISystemLogin SystemLoginComponent
        {
            get;
            set;
        }

        [Dependency]
        public IAuth_User SystemConfigComponent
        {
            get;
            set;
        }

        [Dependency]
        public ITempUserService m_TempUserService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void imgBtnLogin_ServerClick(object sender, EventArgs e)
        {
            try
            {
                //在临时用户信息表中验证用户密码
                string msgString = string.Empty;
                string userID = string.Empty;
                string loginName = this.txtUserID.Value.Trim();
                string userPwd = this.txtPasswd.Value.Trim();
                this.m_TempUserService.CheckLogin(out msgString, out userID, loginName, userPwd);
                if (!string.IsNullOrEmpty(msgString))
                {
                    throw new Exception(msgString);
                }

                //在P_Auth_User表中取用户信息，跳转主页
                User currentUser = SystemLoginComponent.GetCurrentUser(userID);
                Session["CurrentUser"] = currentUser;
                Session["OALoginUserID"] = currentUser.User_ID;
                //记录域密码
                currentUser.Login_Pwd = this.txtPasswd.Value;
                //更新用户的登录时间
                SystemLoginComponent.UpdateUserLoginInfo(userID);

                OnlineUser ou = new OnlineUser();
                #region 记录登录日志
                ILogger log = UContainer.Resolve<ILogger>(Category.LoginLog);
                log.Info(ou.GetLogInfo(currentUser.User_ID,currentUser.User_Name, Request.UserHostAddress, Request.Browser.Platform, Thread.CurrentThread.CurrentUICulture.Name, Request.Browser.Type));
                #endregion

                ou.CheckUserOnLine(
                    currentUser.SessionID, currentUser.User_ID, currentUser.Real_ID, currentUser.User_Name, currentUser.Login_ID, currentUser.Last_Login_Time, currentUser.This_Login_Time, currentUser.ORG_ID);

                if (Request.QueryString["ReturnUrl"] != null && Request.QueryString["ReturnUrl"].ToString().ToLower().IndexOf("default.aspx") == -1 && Request.QueryString["ReturnUrl"].ToString().ToLower().IndexOf("logout.aspx") == -1)
                {
                    FormsAuthentication.RedirectFromLoginPage(userID, false);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(userID, false);
                    Response.Redirect("../../index.aspx");
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }
    }
}